package com.haixiaoke.smartLock.tianwanghulian;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.haixiaoke.common.exception.ServiceException;
import com.haixiaoke.common.utils.RandomNoUtils;
import com.haixiaoke.common.utils.StringUtils;
import com.haixiaoke.domain.*;
import com.haixiaoke.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * 天网互联接口事件处理
 * xiaolong
 * 2023-06-06
 */
@Service
public class SmartLockTwhlEventHandleService {
    @Autowired
    private SmartLockTWHlService smartLockTWHlService;
    @Autowired
    private SmartLockBluetoothServiceImpl smartLockBluetoothService;
    @Autowired
    private SmartLockCardServiceImpl smartLockCardService;
    @Autowired
    private SmartLockFingerprintServiceImpl smartLockFingerprintService;
    @Autowired
    private SmartLockPwdServiceImpl smartLockPwdService;
    @Autowired
    private SmartLockServiceImpl smartLockService;
    @Autowired
    private SmartLockFaceServiceImpl smartLockFaceService;


    /**
     * 新增蓝牙授权信息和授权蓝牙开锁
     *
     * @param smartLockBluetooth 蓝牙授权信息
     * @return 结果
     */
    public int insertSmartLockBluetooth(SmartLockBluetooth smartLockBluetooth) {
        if (StrUtil.isEmpty(smartLockBluetooth.getPhonenumber())) {
            throw new ServiceException("手机号不能为空");
        }
        if (StrUtil.isEmpty(smartLockBluetooth.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (StrUtil.isEmpty(smartLockBluetooth.getIdentification())) {
            throw new ServiceException("智能锁编号不能为空");
        }
        if (smartLockBluetooth.getStartTime() == null) {
            throw new ServiceException("开始时间不能为空");
        }
        if (smartLockBluetooth.getEndTime() == null) {
            throw new ServiceException("结束时间不能为空");
        }
        SmartLock smartLock = new SmartLock();
        smartLock.setIdentification(smartLockBluetooth.getPhonenumber());
        smartLock.setStartTime(smartLockBluetooth.getStartTime());
        smartLock.setEndTime(smartLockBluetooth.getEndTime());
        smartLock.setSn(smartLockBluetooth.getSn());
        String stringResultBean = smartLockTWHlService.addBluetooth(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        //查询蓝牙开锁密钥
        String resultBean = smartLockTWHlService.getUnlockKey(smartLock);
        Integer code1 = (Integer) JSONObject.parseObject(resultBean).get("code");
        if (code1 != 0) {
            String msg = (String) JSONObject.parseObject(resultBean).get("msg");
            throw new ServiceException(msg);
        }
        String bluetoothCookie = (String) JSONObject.parseObject(resultBean).getJSONObject("data").get("bluetoothCookie");
        String bluetoothMac = (String) JSONObject.parseObject(resultBean).getJSONObject("data").get("bluetoothMac");
        String bluetoothPassword = (String) JSONObject.parseObject(resultBean).getJSONObject("data").get("bluetoothPassword");
        smartLockBluetooth.setBluetoothCookie(bluetoothCookie);
        smartLockBluetooth.setBluetoothMac(bluetoothMac);
        smartLockBluetooth.setBluetoothPwd(bluetoothPassword);
        smartLockBluetooth.setIdentification(smartLockBluetooth.getPhonenumber());
        return smartLockBluetoothService.insertSmartLockBluetooth(smartLockBluetooth);
    }

    /**
     * 新增智能锁门卡授权信息和授权门卡
     *
     * @param smartLockCard 智能锁门卡授权信息
     * @return 结果
     */
    public int insertSmartLockCard(SmartLockCard smartLockCard) {
        if (StrUtil.isEmpty(smartLockCard.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (StrUtil.isEmpty(smartLockCard.getIdentification())) {
            throw new ServiceException("智能锁编号不能为空");
        }
        if (smartLockCard.getStartTime() == null) {
            throw new ServiceException("开始时间不能为空");
        }
        if (smartLockCard.getEndTime() == null) {
            throw new ServiceException("结束时间不能为空");
        }
        if (smartLockCard.getCardType() == null) {
            throw new ServiceException("请选择授权门卡类型");
        }
        if (SmartLockCard.IDENTITY_CARD_TYPE.equals(smartLockCard.getCardType())) {
            if (smartLockCard.getCardNumber().length() != 16) {
                throw new ServiceException("卡号输入错误");
            }
        } else {
            if (smartLockCard.getCardNumber().length() != 8) {
                throw new ServiceException("卡号输入错误");
            }
        }
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockCard.getSn());
        smartLock.setIdentification(smartLockCard.getIdentification());
        smartLock.setStartTime(smartLockCard.getStartTime());
        smartLock.setEndTime(smartLockCard.getEndTime());
        smartLock.setCardType(smartLockCard.getCardType());
        smartLock.setCardNumber(smartLockCard.getCardNumber());
        String stringResultBean = smartLockTWHlService.addCard(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        return smartLockCardService.insertSmartLockCard(smartLockCard);
    }

    /**
     * 删除智能锁门卡授权信息和授权门卡授权
     *
     * @param sn             智能锁sn
     * @param identification 门卡标识
     * @return
     */
    public int deleteSmartLockCardBySnAndIdentification(String sn, String identification) {
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(sn);
        smartLock.setIdentification(identification);
        String stringResultBean = smartLockTWHlService.deleteCard(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        return smartLockCardService.deleteSmartLockCardBySnAndIdentification(sn, identification);
    }

    /**
     * 新增智能锁指纹授权信息和授权指纹
     *
     * @param smartLockFingerprint 智能锁指纹授权信息
     * @return 结果
     */
    public int insertSmartLockFingerprint(SmartLockFingerprint smartLockFingerprint) {
        if (StrUtil.isEmpty(smartLockFingerprint.getFingerprint())) {
            throw new ServiceException("指纹数据不能为空");
        }
        if (StrUtil.isEmpty(smartLockFingerprint.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (StrUtil.isEmpty(smartLockFingerprint.getIdentification())) {
            throw new ServiceException("智能锁编号不能为空");
        }
        SmartLock smartLock = new SmartLock();
        smartLock.setFingerprint(smartLockFingerprint.getFingerprint());
        smartLock.setSn(smartLockFingerprint.getSn());
        smartLock.setIdentification(smartLockFingerprint.getIdentification());
        String stringResultBean = smartLockTWHlService.addFingerprint(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        smartLockFingerprint.setSendStatus(SmartLockFingerprint.LOCK_STATE_UN_SEND);
        return smartLockFingerprintService.insertSmartLockFingerprint(smartLockFingerprint);
    }

    /**
     * 批量删除智能锁指纹授权信息和删除指纹授权信息
     *
     * @param fingerprintId 需要删除的智能锁指纹授权信息主键
     * @return 结果
     */
    public int deleteSmartLockFingerprintByFingerprintIds(Long fingerprintId) {
        SmartLockFingerprint smartLockFingerprint = smartLockFingerprintService.selectSmartLockFingerprintByFingerprintId(fingerprintId);
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockFingerprint.getSn());
        smartLock.setIdentification(smartLockFingerprint.getIdentification());
        String stringResultBean = smartLockTWHlService.deleteFingerprint(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        return smartLockFingerprintService.deleteSmartLockFingerprintByFingerprintId(fingerprintId);
    }

    /**
     * 修改智能锁指纹授权信息和下发指纹信息
     *
     * @param smartLockFingerprint 智能锁指纹授权信息
     * @return 结果
     */
    public int updateSmartLockFingerprint(SmartLockFingerprint smartLockFingerprint) {
        if (StrUtil.isEmpty(smartLockFingerprint.getIdentification())) {
            throw new ServiceException("指纹编号不能为空");
        }
        if (StrUtil.isEmpty(smartLockFingerprint.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockFingerprint.getSn());
        smartLock.setIdentification(smartLockFingerprint.getIdentification());
        String stringResultBean = smartLockTWHlService.sendFingerprint(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        smartLockFingerprint.setSendStatus(SmartLockFingerprint.LOCK_STATE_SEND_PROCESS);
        return smartLockFingerprintService.updateSmartLockFingerprint(smartLockFingerprint);
    }

    /**
     * 新增智能锁密码授权信息和智能锁密码授权
     *
     * @param smartLockPwd 智能锁密码授权信息
     * @return 结果
     */
    public int insertSmartLockPwd(SmartLockPwd smartLockPwd) {
        if (StrUtil.isEmpty(smartLockPwd.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (StrUtil.isEmpty(smartLockPwd.getIdentification())) {
            throw new ServiceException("智能锁编号不能为空");
        }
        if (smartLockPwd.getStartTime() == null) {
            throw new ServiceException("开始时间不能为空");
        }
        if (smartLockPwd.getEndTime() == null) {
            throw new ServiceException("结束时间不能为空");
        }
        if (smartLockPwd.getGenerateType() == null) {
            throw new ServiceException("请选择密码生成方式");
        }

        if (SmartLockPwd.PASSWORD_GENERATE_TYPE_SYSGEN.equals(smartLockPwd.getGenerateType())) {
            smartLockPwd.setPassword(RandomNoUtils.getRandomNum(6));
        } else {
            if (StrUtil.isEmpty(smartLockPwd.getPassword())) {
                throw new ServiceException("密码不能为空");
            }
            if (smartLockPwd.getPassword().length() != 6 || !StringUtils.isNumeric(smartLockPwd.getPassword())) {
                throw new ServiceException("请输入6位数字密码");
            }
        }
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockPwd.getSn());
        smartLock.setIdentification(smartLockPwd.getIdentification());
        smartLock.setPassword(smartLockPwd.getPassword());
        smartLock.setStartTime(smartLockPwd.getStartTime());
        smartLock.setEndTime(smartLockPwd.getEndTime());
        String stringResultBean = smartLockTWHlService.addPassword(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");

        //如果智能锁编号相同
        if (code == 5) {
            throw new ServiceException("已有该编号的临时密码");
        }
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        return smartLockPwdService.insertSmartLockPwd(smartLockPwd);
    }

    /**
     * 删除智能锁密码授权信息和删除密码授权
     *
     * @param sn             智能锁sn
     * @param identification 天网互联智能锁标识
     * @return
     */
    public int deleteSmartLockPwdBySnAndIdentification(String sn, String identification) {
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(sn);
        smartLock.setIdentification(identification);
        String stringResultBean = smartLockTWHlService.deletePwd(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        return smartLockPwdService.deleteSmartLockPwdBySnAndIdentification(sn, identification);
    }


    /**
     * 删除所有授权
     *
     * @param smartLock 智能锁设备信息
     * @return
     */
    @Transactional
    public int deleteSmartLockByLockId(SmartLock smartLock) {
        String stringResultBean = smartLockTWHlService.deleteSmartLockByLockId(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        smartLockBluetoothService.deleteSmartLockBluetoothBySn(smartLock.getSn());
        smartLockCardService.deleteSmartLockCardBySn(smartLock.getSn());
        smartLockFingerprintService.deleteSmartLockFingerprintBySn(smartLock.getSn());
        smartLockPwdService.deleteSmartLockPwdBySn(smartLock.getSn());
        return smartLockService.deleteSmartLockByLockId(smartLock.getLockId());
    }

    public int insertSmartLock(SmartLock smartLock) {
        String stringResultBean = smartLockTWHlService.getSmartLock(smartLock);
        Integer total = (Integer) JSONObject.parseObject(stringResultBean).getJSONObject("data").get("total");
        if (total == 0) {
            throw new ServiceException("设备不存在");
        }
        return smartLockService.insertSmartLock(smartLock);
    }

    public Map<String, Object> offlinePassword(SmartLock smartLock) {
        String sn = smartLock.getSn();
        Integer passTime = smartLock.getPassTime();
        if (StrUtil.isEmpty(sn)) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (StrUtil.isEmpty(smartLock.getIdentification())) {
            throw new ServiceException("智能锁编号不能为空");
        }
        if (passTime == null) {
            throw new ServiceException("有效时间不能为空");
        }
        String stringResultBean = smartLockTWHlService.offlinePassword(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        Map<String, Object> Map = new HashMap<>();
        Object password = JSONObject.parseObject(stringResultBean).getJSONObject("data").get("password");
        Map.put("password", password);
        return Map;
    }
}
